package com.castle.fortress.admin.oa.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.annotation.CastleLog;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.oa.entity.UserHolidaySettingEntity;
import com.castle.fortress.admin.oa.dto.UserHolidaySettingDto;
import com.castle.fortress.admin.oa.service.UserHolidaySettingService;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.castle.fortress.admin.utils.ExcelUtils;
import javax.servlet.http.HttpServletResponse;
import com.castle.fortress.common.entity.DynamicExcelEntity;
import java.util.List;

/**
 * 用户假期余额 控制器
 *
 * @author whc
 * @since 2022-11-03
 */
@Api(tags="用户假期余额管理控制器")
@Controller
public class UserHolidaySettingController {
    @Autowired
    private UserHolidaySettingService userHolidaySettingService;

    /**
     * 用户假期余额的分页展示
     * @param userHolidaySettingDto 用户假期余额实体类
     * @param current 当前页
     * @param size  每页记录数
     * @return
     */
    @CastleLog(operLocation = "用户假期余额分页展示", operType = OperationTypeEnum.QUERY)
    @ApiOperation("用户假期余额分页展示")
    @GetMapping("/oa/userHolidaySetting/page")
    @ResponseBody
    @RequiresPermissions("oa:userHolidaySetting:pageList")
    public RespBody<IPage<UserHolidaySettingDto>> pageUserHolidaySetting(UserHolidaySettingDto userHolidaySettingDto, @RequestParam(required = false) Integer current, @RequestParam(required = false)Integer size){
        Integer pageIndex= current==null? GlobalConstants.DEFAULT_PAGE_INDEX:current;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<UserHolidaySettingDto> page = new Page(pageIndex, pageSize);
        IPage<UserHolidaySettingDto> pages = userHolidaySettingService.pageUserHolidaySettingExtends(page, userHolidaySettingDto);

        return RespBody.data(pages);
    }

    /**
     * 用户假期余额的列表展示
     * @param userHolidaySettingDto 用户假期余额实体类
     * @return
     */
    @CastleLog(operLocation = "用户假期余额列表展示", operType = OperationTypeEnum.QUERY)
    @ApiOperation("用户假期余额列表展示")
    @GetMapping("/oa/userHolidaySetting/list")
    @ResponseBody
    public RespBody<List<UserHolidaySettingDto>> listUserHolidaySetting(UserHolidaySettingDto userHolidaySettingDto){
        userHolidaySettingDto.setUserId(WebUtil.currentUser().getId());
        List<UserHolidaySettingDto> list = userHolidaySettingService.listUserHolidaySettingExtends(userHolidaySettingDto);
        return RespBody.data(list);
    }

    /**
     * 用户假期余额保存
     * @param userHolidaySettingDto 用户假期余额实体类
     * @return
     */
    @CastleLog(operLocation = "用户假期余额保存", operType = OperationTypeEnum.INSERT)
    @ApiOperation("用户假期余额保存")
    @PostMapping("/oa/userHolidaySetting/save")
    @ResponseBody
    @RequiresPermissions("oa:userHolidaySetting:save")
    public RespBody<String> saveUserHolidaySetting(@RequestBody UserHolidaySettingDto userHolidaySettingDto){
        if(userHolidaySettingDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        UserHolidaySettingEntity userHolidaySettingEntity = ConvertUtil.transformObj(userHolidaySettingDto,UserHolidaySettingEntity.class);
        if(userHolidaySettingService.save(userHolidaySettingEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 用户假期余额编辑
     * @param userHolidaySettingDto 用户假期余额实体类
     * @return
     */
    @CastleLog(operLocation = "用户假期余额编辑", operType = OperationTypeEnum.UPDATE)
    @ApiOperation("用户假期余额编辑")
    @PostMapping("/oa/userHolidaySetting/edit")
    @ResponseBody
    @RequiresPermissions("oa:userHolidaySetting:edit")
    public RespBody<String> updateUserHolidaySetting(@RequestBody UserHolidaySettingDto userHolidaySettingDto){
        if(userHolidaySettingDto == null || userHolidaySettingDto.getId() == null || userHolidaySettingDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        UserHolidaySettingEntity userHolidaySettingEntity = ConvertUtil.transformObj(userHolidaySettingDto,UserHolidaySettingEntity.class);
        if(userHolidaySettingService.updateById(userHolidaySettingEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 用户假期余额删除
     * @param id
     * @return
     */
    @CastleLog(operLocation = "用户假期余额删除", operType = OperationTypeEnum.DELETE)
    @ApiOperation("用户假期余额删除")
    @PostMapping("/oa/userHolidaySetting/delete")
    @ResponseBody
    @RequiresPermissions("oa:userHolidaySetting:delete")
    public RespBody<String> deleteUserHolidaySetting(@RequestParam Long id){
        if(id == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(userHolidaySettingService.removeById(id)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }


    /**
     * 用户假期余额批量删除
     * @param ids
     * @return
     */
    @CastleLog(operLocation = "用户假期余额批量删除", operType = OperationTypeEnum.DELETE)
    @ApiOperation("用户假期余额批量删除")
    @PostMapping("/oa/userHolidaySetting/deleteBatch")
    @ResponseBody
    @RequiresPermissions("oa:userHolidaySetting:deleteBatch")
    public RespBody<String> deleteUserHolidaySettingBatch(@RequestBody List<Long> ids){
        if(ids == null || ids.size()<1){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(userHolidaySettingService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 用户假期余额详情
     * @param id 用户假期余额id
     * @return
     */
    @CastleLog(operLocation = "用户假期余额详情", operType = OperationTypeEnum.QUERY)
    @ApiOperation("用户假期余额详情")
    @GetMapping("/oa/userHolidaySetting/info")
    @ResponseBody
    @RequiresPermissions("oa:userHolidaySetting:info")
    public RespBody<UserHolidaySettingDto> infoUserHolidaySetting(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        UserHolidaySettingEntity userHolidaySettingEntity = userHolidaySettingService.getById(id);
        UserHolidaySettingDto userHolidaySettingDto = ConvertUtil.transformObj(userHolidaySettingEntity,UserHolidaySettingDto.class);

        return RespBody.data(userHolidaySettingDto);
    }

	/**
     * 动态表头导出 依据展示的字段导出对应报表
     * @param dynamicExcelEntity
     * @param response
     * @throws Exception
     */
    @CastleLog(operLocation = "用户假期余额导出", operType = OperationTypeEnum.EXPORT)
	@PostMapping("/oa/userHolidaySetting/exportDynamic")
	@ApiOperation("动态表头导出，依据展示的字段导出对应报表")
	public void exportDynamic(@RequestBody DynamicExcelEntity<UserHolidaySettingDto> dynamicExcelEntity, HttpServletResponse response) throws Exception {
		List<UserHolidaySettingDto> list = userHolidaySettingService.listUserHolidaySetting(dynamicExcelEntity.getDto());
		//字典、枚举、接口、json常量等转义后的列表数据 根据实际情况初始化该对象，null为list数据直接导出
		List<List<Object>> dataList = null;
		/**
        * 根据实际情况初始化dataList,可参照 com.castle.fortress.admin.system.controller.TmpDemoController类中方法：exportDynamic
        */
		ExcelUtils.exportDynamic(response,dynamicExcelEntity.getFileName()+".xlsx",null,list,dynamicExcelEntity.getHeaderList(),dataList);
	}


}
